package edu.rjxy.xjdx.emps.servlet;

import edu.rjxy.xjdx.emps.common.JSONUtil;
import edu.rjxy.xjdx.emps.common.domain.Employee;
import edu.rjxy.xjdx.emps.service.EmployeeService;
import edu.rjxy.xjdx.emps.service.EmployeeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/submitEmpEdit")
public class SubmitEmpEditServlet extends HttpServlet {
    EmployeeService employeeService = new EmployeeServiceImpl();
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String idTxt = request.getParameter("id").trim();
        int id = Integer.parseInt(idTxt);
        String name = request.getParameter("name");
        String personNumber = request.getParameter("personNumber");
        String sex = request.getParameter("sex");
        sex = (sex.equals("0")?"女":"男");
        Employee employee  = new Employee();
        employee.setName(name.trim());
        employee.setPersonNumber(personNumber.trim());
        employee.setSex(sex.trim());
        employee.setId(id);

        int count =  employeeService.updateEmployee(employee);
        Map<String,Object> map = new HashMap<>();
        map.put("success",true);
        map.put("count",count);
        JSONUtil.printJSON(map,response);
    }
}
